'use strict';

const RouterService = require('../base');
const { Op } = require('sequelize');

class DishRatingService extends RouterService {
  static get modelName() {
    return 'Business.DishRating';
  }

  async beforeCreate(payload) {
    const { category, dish_name } = payload;
    // 不能重复
    const dishRating = await this.model.findOne({
      where: {
        category,
        dish_name,
        deleted: 0
      }
    });
    if (dishRating) {
      throw new Error('菜品评价选项已存在');
    }
    return true
  }
  async beforeUpdate(id, payload) {
    const { category, dish_name } = payload;
    // 不能重复
    const dishRating = await this.model.findOne({
      where: {
        category,
        dish_name,
        deleted: 0,
        // 排除id
        id: {
          [Op.ne]: id
        }
      }
    });
    if (dishRating) {
      throw new Error('菜品评价选项已存在');
    }
    return true
  }
}

module.exports = DishRatingService; 